package com.example.weather_app.city_manager;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.weather_app.R;
import com.example.weather_app.db.DBManager;
import com.example.weather_app.myadapter.DeleteCityAdapter;

import java.util.ArrayList;
import java.util.List;

public class DeleteCityActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView errorIv, rightIv;
    private ListView deleteLv;
    private List<String> mDatas = DBManager.queryAllCityName();;
    private List<String> deleteCitys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_city);

        initPager();

        //设置监听
        errorIv.setOnClickListener(this);
        rightIv.setOnClickListener(this);
        //设置适配器
        DeleteCityAdapter deleteCityAdapter = new DeleteCityAdapter(this, mDatas, deleteCitys);
        deleteLv.setAdapter(deleteCityAdapter);
//        deleteCityAdapter.notifyDataSetChanged();
    }

    private void initPager() {
        errorIv = findViewById(R.id.delete_iv_error);
        rightIv = findViewById(R.id.delete_iv_right);
        deleteLv = findViewById(R.id.delete_lv);
        deleteCitys = new ArrayList<>();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.delete_iv_error:
                //取消删除,设置弹出框提示
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示信息").setMessage("是否取消删除操作")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                finish();
                            }
                        })
                        .setNegativeButton("取消", null);
                builder.create().show();
                break;
            case R.id.delete_iv_right:
                //确认删除,对数据库进行操作
                for (int i = 0; i < deleteCitys.size(); i++) {
                    String city = deleteCitys.get(i);
                    DBManager.deleteInfoByCity(city);
                }
                //返回上一级页面
                finish();
                break;
        }
    }
}